# -*- coding: utf-8 -*-
# Copyright 2017-2019 ControlScan, Inc.
#
# This file is part of Cyphon Engine.
#
# Cyphon Engine is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Cyphon Engine is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Cyphon Engine. If not, see <http://www.gnu.org/licenses/>.
"""
Defines a custom permission class.
"""

# third party
from django_filters.rest_framework import DjangoFilterBackend


class ContextFilterFilterBackend(DjangoFilterBackend):
    """

    """

    def filter_queryset(self, request, queryset, view):
        """

        """
        user = request.user

        if not user.is_staff:
            if user.company:
                return queryset.filter(context__primary_distillery__company=user.company,
                                       context__related_distillery__company=user.company)\
                               .distinct()
            else:
                return queryset.none()
        else:
            return queryset


class ContextFilterBackend(DjangoFilterBackend):
    """

    """

    def filter_queryset(self, request, queryset, view):
        """

        """
        user = request.user

        if not user.is_staff:
            if user.company:
                return queryset.filter(primary_distillery__company=user.company,
                                       related_distillery__company=user.company)\
                               .distinct()
            else:
                return queryset.none()
        else:
            return queryset

